package com.xx.xiawan.net;

import com.android.volley.AuthFailureError;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017-08-30.
 */

public class DGStringRequest extends StringRequest {
    private Map<String, String> mParamsMap = new HashMap<String,String>();
    private Map<String, String> mHeaderMap = new HashMap<String, String>();
    public DGStringRequest(String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(url, listener, errorListener);
    }

    public DGStringRequest(int method,String url, Response.Listener<String> listener, Response.ErrorListener errorListener) {
        super(method,url, listener, errorListener);
    }

    protected Map<String, String> getParams() throws AuthFailureError {
        return mParamsMap;
    }
//    public Map<String, String> getHeaders() throws AuthFailureError {
//        return mHeaderMap;
//    }


    public void header(String name, String value) {
        mHeaderMap.put(name, value);
    }


    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        if (mHeaderMap != null && mHeaderMap.size() > 0) {
            Map<String, String> headers = new HashMap<String, String>();
            headers.put("Charset", "UTF-8");
            headers.put("Content-Type", "application/json");
            for(Map.Entry<String,String> entry:mHeaderMap.entrySet()){
                headers.put(entry.getKey(), entry.getValue());
            }
            return headers;
        } else {
            return super.getHeaders();
        }
    }

    public void setParamsMap(Map<String, String> paramsMap){
        if(paramsMap != null){
            for (Map.Entry<String, String> entry : paramsMap.entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                mParamsMap.put(entry.getKey(),entry.getValue());
            }
        }
    }

    public void setHeaderMap(Map<String, String> headerMap){
        if(headerMap != null){
            for (Map.Entry<String, String> entry : headerMap.entrySet()) {
                System.out.println("Key = " + entry.getKey() + ", Value = " + entry.getValue());
                mHeaderMap.put(entry.getKey(),entry.getValue());
            }
        }
    }
}
